/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 21, 2012  12:34:44 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : SdContarctUtilityServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.SdContarctUtility;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class SdContarctUtilityServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(SdContarctUtilityServices.class);
	
	/**
	 * @param em
	 */
	public SdContarctUtilityServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}

	public SdContarctUtility findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "SdContarctUtility.findById"));				
	}
	
	public List<SdContarctUtility> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "SdContarctUtility.findByInstanceNo"));				
	}
	
	public List<SdContarctUtility> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdContarctUtility.findIsSync", mapWhereValue));				
	}
	
	public List<SdContarctUtility> findByRefReceipted(String company, String project, String documentNo, String status, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		mapWhereValue.put("status", status);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdContarctUtility.findByRefReceipted", mapWhereValue));			
	}
	
	public SdContarctUtility Updated(SdContarctUtility value) throws Exception {
		if(value != null)
		{
			return (SdContarctUtility) super.updated(value);
		}else
			return null;
	}
	
	public SdContarctUtility InsertedOrUpdated(SdContarctUtility vlaue) throws Exception {
		SdContarctUtility obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("SD_CONTARCT_UTILITIES");					
				obj.setPromisStagingId(id);							
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<SdContarctUtility> Inserted(List<SdContarctUtility> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdContarctUtility obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("SD_CONTARCT_UTILITIES");					
					obj.setPromisStagingId(id);								
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<SdContarctUtility> Updated(List<SdContarctUtility> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdContarctUtility obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
